package org.exhaust.framework.net.common.protocol.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.TypeParameterMatcher;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.common.protocol.BaseFrame;

/**
 * 数据事件处理器
 */
@Slf4j
public abstract class BusinessDataHandler<T extends BaseFrame> {

    private final TypeParameterMatcher matcher;

    protected BusinessDataHandler() {
        matcher = TypeParameterMatcher.find(this, BusinessDataHandler.class, "T");
    }

    public void receiveMsg(ChannelHandlerContext ctx, BaseFrame packet) {
        if (matcher.match(packet)) {
            this.handleMsg(ctx, (T) packet);
        } else {
            log.warn("Type miss match！Decoded msg class name:{}", packet.getClass().getName());
        }
    }

    public abstract void handleMsg(ChannelHandlerContext ctx, T packet);
}
